package com.lianzhihui.romeex.ui.me;

import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;

import com.lianzhihui.romeex.MyApplication;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.InvitationInfoBean;
import com.lianzhihui.romeex.bean.UserInfoBean;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.mvp.presenter.InvitationPresnterImp;
import com.lianzhihui.romeex.mvp.view.InvitationView;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.ui.me.dialog.ShareDialog;
import com.lianzhihui.romeex.utils.ImagesUtil;
import com.lianzhihui.romeex.utils.IntentUtils;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.QrUtils.QRUtils;
import com.lianzhihui.romeex.utils.StatusBarUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * @ProjectName: bhex-broker-app-android-master
 * @Package: io.bhex.app.buy
 * @ClassName: InvitationActivity
 * @Description: 邀请界面
 * @Author: zhanghong
 * @CreateDate: 2020/10/12 18:51
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/10/12 18:51
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class InvitationActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks, InvitationView {

    @BindView(R.id.state_bar)
    View stateBar;

    @BindView(R.id.root_layout_view)
    NestedScrollView rootLayoutView;
    @BindView(R.id.include_top_iv_back)
    ImageView includeTopIvBack;
    @BindView(R.id.include_top_lin_newback)
    LinearLayout includeTopLinNewback;
    @BindView(R.id.include_top_tv_title)
    TextView includeTopTvTitle;
    @BindView(R.id.include_top_iv_right_second)
    ImageView includeTopIvRightSecond;
    @BindView(R.id.include_top_tv_right)
    TextView includeTopTvRight;
    @BindView(R.id.include_top_lin_right)
    LinearLayout includeTopLinRight;
    @BindView(R.id.include_top_lin_background)
    LinearLayout includeTopLinBackground;
    @BindView(R.id.app_top_bar)
    LinearLayout appTopBar;
    @BindView(R.id.tv_link)
    TextView tvLink;
    @BindView(R.id.inv_code)
    TextView invCode;
    @BindView(R.id.tv_share)
    TextView tvShare;
    @BindView(R.id.qr_image)
    ImageView qrImage;
    View saveView;
    private static final int REQUEST_CODE_QRCODE_PERMISSIONS = 1;

    private ShareDialog shareDialog;
    private String inviteCode;
    private InvitationInfoBean info;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_invitation_layout;
    }

    @Override
    protected void initView() {

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, stateBar.getHeight());
        layoutParams.topMargin = StatusBarUtil.getStatusBarHeight(MyApplication.mContext);
        stateBar.setLayoutParams(layoutParams);
        includeTopIvBack.setImageResource(R.drawable.icon_return_w);
        includeTopLinBackground.setBackgroundColor(getResources().getColor(R.color.transparent));
        includeTopTvRight.setText(getString(R.string.text_inv_rule));
        includeTopTvRight.setTextColor(getResources().getColor(R.color.white));
        StatusBarUtil.setTransparentForImageView(this, null);
    }

    @Override
    public boolean setFitSystemWindowFlag() {
        return false;
    }

    @Override
    protected void initData() {
        Bundle extras = getIntent().getExtras();
        InvitationPresnterImp invitationPresnterImp = new InvitationPresnterImp(this, this);
        if (extras != null) {
            inviteCode = extras.getString(AppConstants.INTENT_INVITATION_CODE, "");
            if (!inviteCode.isEmpty()) {
                invCode.setText(inviteCode);
                ImagesUtil.createQrCodeImg(QRUtils.type4 + inviteCode, qrImage, 500);
            }
        } else {
            invitationPresnterImp.requestHomeInfo();
        }
        invitationPresnterImp.requestBindInfo();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        dismissLoading();
        ImagesUtil.saveQrCode(this, saveView);
//        toast(getString(R.string.text_string_save_success));
        shareDialog.dismiss();
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {

    }


    @AfterPermissionGranted(REQUEST_CODE_QRCODE_PERMISSIONS)
    private void requestCodeQRCodePermissions(View v) {
        String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (!EasyPermissions.hasPermissions(this, perms)) {
            EasyPermissions.requestPermissions(this, getResources().getString(R.string.text_permissions), REQUEST_CODE_QRCODE_PERMISSIONS, perms);
        } else {
            dismissLoading();
            ImagesUtil.saveQrCode(this, v);
//            toast(getString(R.string.text_string_save_success));
            shareDialog.dismiss();
        }
    }

    @OnClick({R.id.include_top_iv_back, R.id.tv_share, R.id.inv_code, R.id.include_top_tv_right})
    public void OnClick(View v) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            switch (v.getId()) {
                case R.id.include_top_iv_back:
                    finish();
                    break;
                case R.id.tv_link:

                    break;
                case R.id.include_top_tv_right:
                    if (info != null && info.getList().size() > 0) {
                        IntentUtils.JumpToHaveObj(InvitationRuleActivity.class, AppConstants.INTENT_INV_RULE, info);
                    }
                    break;
                case R.id.tv_share:
                    shareDialog = new ShareDialog(this, inviteCode);
                    shareDialog.setCallback(view -> {

                        showLoading("");
                        saveView = v;
                        requestCodeQRCodePermissions(view);

                    });
                    shareDialog.show();
                    shareDialog.setCancelable(true);
                    break;
                case R.id.inv_code:
                    copyString(invCode.getText().toString());
                    break;

                default:
                    break;
            }
        }
    }


    @Override
    public void success(UserInfoBean data) {
        inviteCode = data.getInviteCode();
        invCode.setText(inviteCode);
        ImagesUtil.createQrCodeImg(QRUtils.type4 + inviteCode, qrImage, 500);
    }

    @Override
    public void failed(int errorCode, String errorStr) {

    }

    @Override
    public void responseBindInfoSuccess(InvitationInfoBean b) {
        if (b == null) {
            return;
        }
        InvitationInfoBean.ListBean listBean = new InvitationInfoBean.ListBean();
        listBean.setKeyInfo(getResources().getString(R.string.text_dir_people));
        listBean.setKeyValue(getResources().getString(R.string.text_earnings));
        b.getList().add(0, listBean);
        info = b;

    }

    @Override
    public void setFailure() {

    }

}
